// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Net.Sockets;

namespace SoapTrace.Core.Communication
{
	/// <summary>
	/// The proxy server that handles soap messages.
	/// </summary>
	public class SoapMessageProxyServer : ProxyServer
	{
		/// <summary>
		/// Soap request received from the client event.
		/// </summary>
		public event SoapRequestMessageReceivedEventHandler SoapRequestReceived;

		/// <summary>
		/// Soap response received from the server event.
		/// </summary>
		public event SoapResponseMessageReceivedEventHandler SoapResponseReceived;

		/// <summary>
		/// Creates a new instance of a <see cref="SoapMessageProxyServer"/>
		/// class.
		/// </summary>
		public SoapMessageProxyServer( )
		{
		}
		
		/// <summary>
		/// Creates a <see cref="ProxyServerClientConnection"/> object.
		/// </summary>
		/// <param name="socket">The accepted client socket.</param>
		/// <returns>A <see cref="ProxyServerClientConnection"/> object.</returns>
		protected override TcpSocketConnection CreateClientConnection( Socket socket )
		{
			ProxyServerClientConnection ClientConnection = 
				new ProxyServerClientConnection( socket );

			ClientConnection.SoapRequestReceived += 
				new SoapRequestMessageReceivedEventHandler( OnSoapRequestReceived );

			ClientConnection.SoapResponseReceived += 
				new SoapResponseMessageReceivedEventHandler( OnSoapResponseReceived );

			return ClientConnection;
		}

		/// <summary>
		/// Creates a server connection.
		/// </summary>
		/// <param name="address">The server's IP address.</param>
		/// <param name="port">The server's port.</param>
		/// <returns>A server connection.</returns>
		protected override TcpSocketConnection CreateServerConnection( string address,
		                                                        int port )
		{
			return new ServerConnection( address, port );
		}
		
		/// <summary>
		/// Soap request message received from a client connection.
		/// </summary>
		/// <param name="sender">The event source.</param>
		/// <param name="e">A <see cref="SoapRequestReceivedEventArgs"/> object.</param>
		private void OnSoapRequestReceived( object sender, SoapRequestReceivedEventArgs e )
		{
			if( SoapRequestReceived != null )
			{
				SoapRequestReceived( sender, e );
			}
		}

		/// <summary>
		/// Soap response message received from a client connection.
		/// </summary>
		/// <param name="sender">The event source.</param>
		/// <param name="e">A <see cref="SoapResponseReceivedEventArgs"/> object.</param>
		private void OnSoapResponseReceived( object sender, SoapResponseReceivedEventArgs e )
		{
			if( SoapResponseReceived != null )
			{
				SoapResponseReceived( sender, e );
			}
		}
	}
}
